<?php
namespace Frontend\Controllers;

use Models\Accesstoken;
use Models\User;
use Models\Wx;
use Models\Alipay;

class AppmainController extends BaseController
{
    public function indexAction($accesstoken)
    {
        $token = Accesstoken::findFirstByaccesstoken($accesstoken);
        if ($token) {
            $user = User::findFirstByuid($token->uid);
            echo json_encode(array('accesstoken' => $accesstoken, 'username' => $user->username));
        } else {
            echo json_encode(array('accesstoken' => "no access"));
        }
    }

    public function gettokenAction($accesstoken)
    {
        $token = Accesstoken::findFirstByaccesstoken($accesstoken);
        if ($token) {
            $wx = Wx::findFirstByuid($token->uid);
            if ($wx) {
                echo json_encode(array('accesstoken' => $accesstoken, 'token' => $wx->token));
            } else {
                echo json_encode(array('accesstoken' => 'no token'));
            }
        } else {
            echo json_encode(array('accesstoken' => "no access"));
        }
    }

    public function getappidAction($accesstoken)
    {
        $token = Accesstoken::findFirstByaccesstoken($accesstoken);
        if ($token) {
            $Alipay = Alipay::findFirstByuid($token->uid);
            if ($Alipay) {
                echo json_encode(array('accesstoken' => $accesstoken, 'appid' => $Alipay->appid));
            } else {
                echo json_encode(array('accesstoken' => 'no appid'));
            }
        } else {
            echo json_encode(array('accesstoken' => "no access"));
        }
    }
}